//
//  CCCustomView.swift
//  SinaWebo
//
//  Created by 陶建 on 16/3/28.
//  Copyright © 2016年 taoJ. All rights reserved.
//

import UIKit

protocol CCLogoutViewDelegate: NSObjectProtocol {
    func logoutViewDidRegisterButton(registerButton: UIButton)
    func logoutViewDidLoginButton(loginButton: UIButton)
}

/// 未登录View
class CCLogoutView: UIView {
    weak var delegate: CCLogoutViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 添加一个大的View 用来上下对其
        addSubview(contentView)
        // 后面旋转的图片
        contentView.addSubview(rotaryImage)
        // 遮住旋转图片的图片
//        addSubview(coverImage)
        // 小房子图片
        addSubview(houseImage)
        // 文字label
        addSubview(titleLabel)
        // 注册按钮
        addSubview(registerButton)
        // 登陆按钮
        addSubview(loginButton)
        
        // 动画让旋转的图片动起来
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 无限次
        anim.repeatCount = MAXFLOAT
        // 20秒执行1次
        anim.duration = 20
        // 转360°
        anim.toValue = M_PI * 2
        // 不反弹的旋转
        anim.removedOnCompletion = false
        rotaryImage.layer.addAnimation(anim, forKey: nil)
    }
    
    // 判断不同界面显示的不同文字, 隐藏图片
    func setupVisitorInfo(isHome: Bool, imageName: String, title: String) {
        rotaryImage.hidden = !isHome
         coverImage.hidden = !isHome
        titleLabel.text = title
        houseImage.image = UIImage(named: imageName)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /**
     布局
     */
    override func layoutSubviews() {
        super.layoutSubviews()
        
        contentView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            make.width.equalTo(300)
            make.height.equalTo(280)
        }
        layoutIfNeeded()
        
        rotaryImage.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.top.equalTo(contentView.snp_top)
        }
        
//         coverImage.snp_makeConstraints { (make) in
//            make.center.equalTo(rotaryImage)
//            make.size.equalTo(contentView)
//         }
//
        houseImage.snp_makeConstraints { (make) in
            make.center.equalTo(rotaryImage)
        }

        titleLabel.snp_makeConstraints { (make) in
            make.top.equalTo(rotaryImage.snp_bottom)
            make.width.equalTo(224)
            make.centerX.equalTo(self)
        }

        registerButton.snp_makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp_bottom).offset(10)
            make.left.equalTo(titleLabel.snp_left).offset(-10)
            make.width.equalTo(titleLabel).multipliedBy(0.5)
            make.height.equalTo(40)
        }
        
        loginButton.snp_makeConstraints { (make) in
            make.top.equalTo(registerButton)
            make.right.equalTo(titleLabel.snp_right).offset(10)
            make.width.equalTo(registerButton)
            make.height.equalTo(registerButton)
        }
    }
    
    private lazy var contentView: UIView = {
        let contentView = UIView()
        contentView.backgroundColor = UIColor.clearColor()
        return contentView
    }()
    
    
    private lazy var rotaryImage: UIImageView = {
        let rotaryImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return rotaryImage
    }()
    
    private lazy var coverImage: UIImageView = {
        let coverImage = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return coverImage
    }()
    
    private lazy var houseImage: UIImageView = {
        let houseImage = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        
        return houseImage
    }()
    
    private lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.text = ""
        titleLabel.sizeToFit()
        titleLabel.numberOfLines = 0
        
        return titleLabel
    }()
    
    lazy var registerButton: UIButton = {
        let registerButton = UIButton()
        registerButton.setTitle("注册", forState: .Normal)
        registerButton.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        registerButton.addTarget(self, action: #selector(CCLogoutView.tapRegisterButton(_:)), forControlEvents: .TouchUpInside)
        registerButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        registerButton.setBackgroundImage(UIImage(named:"common_button_alpha_highlighted"), forState: .Highlighted)
        
        return registerButton
    }()
    
    func tapRegisterButton(button: UIButton) {
        delegate?.logoutViewDidRegisterButton(button)
    }
    
    private lazy var loginButton: UIButton = {
        let loginButton = UIButton()
        loginButton.setTitle("登陆", forState: .Normal)
        loginButton.addTarget(self, action: #selector(CCLogoutView.tapLoginButton(_:)), forControlEvents: .TouchUpInside)
        loginButton.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        loginButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Normal)
        loginButton.setBackgroundImage(UIImage(named:"common_button_alpha_highlighted"), forState: .Highlighted)
        
        return loginButton
    }()
    
    func tapLoginButton(button: UIButton) {
        delegate?.logoutViewDidLoginButton(button)
    }
}
